/*
 *
 * (c) Copyright Ascensio System Limited 2010-2023
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Web;

using ASC.Core;
using ASC.Web.Core.Client.HttpHandlers;
using ASC.Web.Studio.Core.Users;
using ASC.Web.Studio.PublicResources;

namespace ASC.Web.Studio.Masters.MasterResources
{
    public class MasterLocalizationResources : ClientScriptLocalization
    {
        private static string GetDatepikerDateFormat(string s)
        {
            return s
                .Replace("yyyy", "yy")
                .Replace("yy", "yy")
                .Replace("MMMM", "MM")
                .Replace("MMM", "M")
                .Replace("MM", "mm")
                .Replace("M", "mm")
                .Replace("dddd", "DD")
                .Replace("ddd", "D")
                .Replace("dd", "11")
                .Replace("d", "dd")
                .Replace("11", "dd")
                .Replace("'", "")
                ;
        }

        protected override string BaseNamespace
        {
            get { return "ASC.Resources.Master"; }
        }

        protected override IEnumerable<KeyValuePair<string, object>> GetClientVariables(HttpContext context)
        {
            var dateTimeFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat;

            return new List<KeyValuePair<string, object>>(5)
            {
                RegisterResourceSet("ResourceJS", ResourceJS.ResourceManager),
                RegisterResourceSet("FeedResource", FeedResource.ResourceManager),
                RegisterResourceSet("ChatResource", ChatResource.ResourceManager),
                RegisterResourceSet("UserControlsCommonResource", UserControlsCommonResource.ResourceManager),
                RegisterObject(
                    new
                    {
                        TemplateResource = new
                        {
                            Resource.Administrator,
                            Resource.Owner,
                            Resource.Comments,
                            Resource.AddNewCommentButton,
                            Resource.PublishButton,
                            Resource.PreviewButton,
                            Resource.CancelButton,
                            Resource.AddButton,
                            Resource.CloseButton,
                            Resource.AnswerButton,
                            Resource.EditButton,
                            Resource.DeleteButton,
                            Resource.CommentWasRemoved,
                            Resource.AssociateAccountConnected,
                            Resource.AssociateAccountNotConnected,
                            Resource.AssociateAccountDisconnect,
                            Resource.AssociateAccountConnect,
                            Resource.MailIcsCalendarWhenLabel,
                            Resource.MailIcsCalendarRecurrenceLabel,
                            Resource.MailIcsCalendarWhereLabel,
                            Resource.MailIcsCalendarMapLabel,
                            Resource.MailIcsCalendarNoLocationLabel,
                            Resource.MailIcsCalendarWhoLabel,
                            Resource.MailIcsCalendarDescriptionLabel,
                            Resource.MailIcsCalendarOrganizerLabel,
                            Resource.MailAutoGeneratedText,
                            Resource.ClearFilterButton,
                            Resource.Show,
                            Resource.Hide,
                            Resource.DrnToday,
                            Resource.DrnTomorrow,
                            Resource.In,
                            Yet2 = DateTimeExtension.Yet(2),
                            Yet3 = DateTimeExtension.Yet(3),
                            AllRightsReservedText = string.Format(CultureInfo.InvariantCulture, CoreContext.Configuration.CustomMode ? CustomModeResource.AllRightsReservedCustomMode : Resource.AllRightsReservedText, DateTime.UtcNow.Year),
                            Resource.LdapUsersListLockTitle,
                            Resource.LdapUserEditCanOnlyAdminTitle,
                            Resource.LdapSettingsSuccess,
                            Resource.Browser,
                            Resource.SsoUsersListLockTitle,
                            Resource.SsoUserEditCanOnlyAdminTitle,
                            Resource.TfaAppShowBackupCodesNote,
                            Resource.TfaAppShowBackupCodesDescription,
                            Resource.PrintButton,
                            Resource.RequestNewButton,
                            Resource.Choose,
                            Resource.ConsumersS3EncryprionNone,
                            Resource.ConsumersS3EncryprionSSE,
                            Resource.ConsumersS3EncryprionCSE,
                            Resource.ConsumersS3EncryprionSSES3,
                            Resource.ConsumersS3EncryprionSSES3Help,
                            Resource.ConsumersS3EncryprionSSEKMS,
                            Resource.ConsumersS3EncryprionSSEKMSHelp,
                            Resource.ConsumersS3EncryprionCMK,
                            Resource.ConsumersS3EncryprionCMKDefault,
                            Resource.ConsumersS3EncryprionCMKCustom,
                            Resource.ConsumersS3EncryprionKey
                        },

                        DatePattern = dateTimeFormat.ShortDatePattern.Replace("'", ""),
                        TimePattern = dateTimeFormat.ShortTimePattern,
                        DateTimePattern = dateTimeFormat.FullDateTimePattern.Replace("'", ""),
                        DatePatternJQ = DateTimeExtension.DateMaskForJQuery,
                        //.Replace(" ", string.Empty) -  remove because, crash date in datepicker on czech language (bug 21954)
                        DatepickerDatePattern = GetDatepikerDateFormat(dateTimeFormat.ShortDatePattern),
                        DatepickerTimePattern = GetDatepikerDateFormat(dateTimeFormat.ShortTimePattern),
                        DatepickerDateTimePattern = GetDatepikerDateFormat(dateTimeFormat.FullDateTimePattern),
                        FirstDay = (int) dateTimeFormat.FirstDayOfWeek,
                        DayNames = dateTimeFormat.AbbreviatedDayNames,
                        DayNamesFull = dateTimeFormat.DayNames,
                        MonthNames = Thread.CurrentThread.CurrentUICulture.DateTimeFormat.AbbreviatedMonthGenitiveNames,
                        MonthNamesFull = Thread.CurrentThread.CurrentUICulture.DateTimeFormat.MonthNames,
                        Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName,
                        CurrentCultureName = Thread.CurrentThread.CurrentCulture.Name.ToLowerInvariant(),
                        CurrentCulture = CultureInfo.CurrentCulture.Name,
                        Resource.FileSizePostfix,
                        Resource.AccessRightsAccessToProduct,
                        Resource.AccessRightsDisabledProduct,
                        Resource.EmailAndPasswordIncorrectEmail,
                        Resource.EmailIsAvailable,
                        Resource.EmailOnDomainIsNotCreated,
                        Resource.NoMailServerDomainsMsg,
                        Resource.EmailAndPasswordCopiedToClipboard,
                        Resource.CopyToClipboard,
                        Resource.QuotaSettingsEditQuota,
                        Resource.QuotaSettingsNoQuota
                    })
            };
        }
    }

    public class MasterCustomResources : ClientScriptCustom
    {
        protected override string BaseNamespace
        {
            get { return "ASC.Resources.Master"; }
        }

        protected override IEnumerable<KeyValuePair<string, object>> GetClientVariables(HttpContext context)
        {
            return new List<KeyValuePair<string, object>>(1)
                   {
                       RegisterObject(
                       new
                            {
                                Admin = CustomNamingPeople.Substitute<Resource>("Administrator"),
                                User = CustomNamingPeople.Substitute<Resource>("User"),
                                Guest = CustomNamingPeople.Substitute<Resource>("Guest"),
                                Department = CustomNamingPeople.Substitute<Resource>("Department"),
                                ConfirmRemoveUser = CustomNamingPeople.Substitute<Resource>("DeleteUserConfirmation").HtmlEncode(),
                                ConfirmRemoveDepartment = CustomNamingPeople.Substitute<Resource>("DeleteDepartmentConfirmation").HtmlEncode(),
                                AddDepartmentHeader = CustomNamingPeople.Substitute<Resource>("AddDepartmentDlgTitle").HtmlEncode(),
                                EditDepartmentHeader = CustomNamingPeople.Substitute<Resource>("DepEditHeader").HtmlEncode(),
                                EmployeeAllDepartments = CustomNamingPeople.Substitute<Resource>("EmployeeAllDepartments").HtmlEncode(),
                                AddEmployees = CustomNamingPeople.Substitute<UserControlsCommonResource>("AddEmployees").HtmlEncode(),
                                AccessRightsAddUser = CustomNamingPeople.Substitute<Resource>("AccessRightsAddUser").HtmlEncode(),
                                AccessRightsAddGroup = CustomNamingPeople.Substitute<Resource>("AccessRightsAddGroup").HtmlEncode(),
                                AccessRightsAllUsers = CustomNamingPeople.Substitute<Resource>("AccessRightsAllUsers"),
                                AccessRightsUsersFromList = CustomNamingPeople.Substitute<Resource>("AccessRightsUsersFromList"),
                                AccessRightsEmptyUserList = CustomNamingPeople.Substitute<Resource>("AccessRightsEmptyUserList"),
                                ChooseUser = CustomNamingPeople.Substitute<Resource>("ChooseUser"),
                                ErrorEmailAlreadyExists = CustomNamingPeople.Substitute<Resource>("ErrorEmailAlreadyExists")
                            })
                   };
        }
    }
}